package service.impl;

import dao.BookDao;
import dao.impl.BookDaoImpl;
import entity.Book;
import service.BookService;
import util.PageHelper;

import java.util.List;

/**
 * User: key
 * ClassName:BookServiceImpl
 * Date: 2023/5/5
 * Time: 18:50
 * description:
 */
public class BookServiceImpl implements BookService {
    private BookDao bd = new BookDaoImpl();
    @Override
    public boolean insertBook(Book b) {
        try {
            if(bd.addBook(b)>0){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Book getOneById(int id) {
        try {
            if(id>0){
                return bd.getOneById(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean delBook(int id) {
        try {
            if(bd.delBook(id)>0){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public PageHelper<Book> getByPage(int pageNum, int pageSize) {
        try {
            int count = bd.getCount().intValue();
            PageHelper helper = new PageHelper(pageNum,pageSize,count);
            List<Book> byPage = bd.getByPage(helper.getStartRow(),pageSize);
            helper.setList(byPage);
            return helper;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Book> getAll() {
        try {
            return bd.getAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
